<?php
class RelationModel extends MongoModel {
	var $table = 'relation';
    var $name = '关联属性';
	
	public static function get_base_columns(){
		return array('code', 'name', 'desc', 'site_id', '_id', 'owner');
	}
	
	public function get_relation_by_code($code, $langs=null){
		$relateion = $this->get_by_attrs(array('code'=>$code));
		return self::get_list($relateion, $langs);
	}
	
	public function get_list($id, $langs=null){
		if(is_string($id)){
			$relations = self::get_by_key($id);
		}else{
			$relations = $id;
		}

		if($langs === null){
			$langs = Lang::get_langs();
		}
		if(empty($relations)) return null;
		$deleted_column = RelationModel::get_base_columns();
		if($langs!==null){
			//语言包数据
			$deleted_column = array_merge($deleted_column, $langs);
		}
		
		foreach ($deleted_column as $c){
			unset($relations[$c]);
		}
		
		$relation_list = array();
		foreach ($relations as $k=>$r){
			$relation_list[$k] = $r;
		}
		return $relation_list;
	}
	
	public function get_options($site_id=null){
		if($site_id == null){
			//未指定网站
			$rs = self::find(array('$or'=>array(array('site_id'=>SITE_ID),array('owner'=>'system'))), LANGUAGE);
		}else{
			$rs = self::find(array('site_id'=>$site_id), LANGUAGE);
		}
		$options = array();
		foreach($rs as $k=>$r){
			$options[$k] = $r[LANGUAGE]['name'];
		}
		return $options;
	}
}

?>